package org.farm.pojo;

import lombok.Data;
import org.farm.Enum.ResultCodeEnum;

import java.util.Map;

@Data
public class ResultVo<T>{

    private Integer code;
    private String message;
    private T data;

    public ResultVo(){}

    public ResultVo(ResultCodeEnum resultCode, String customMessage, T data) {
        this.code = resultCode.getCode();
        this.message = customMessage;
        this.data = data;
    }

//    public static final Integer SUCCESS = 200;
//    public static final Integer FAIL = 400;

    // 返回数据
    protected static<T> ResultVo<T> build(T data) {

        ResultVo<T> result = new ResultVo();

        if (data != null)
            result.setData(data);

        return result;
    }

    public static <T> ResultVo<T> build(T body, Integer code, String message) {
        ResultVo<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> ResultVo<T> build(T body, ResultCodeEnum resultCodeEnum) {
        ResultVo<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static<T> ResultVo<T> success(String message,T data) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(200);
        resultVo.setData(data);
        resultVo.setMessage(message);
        return resultVo;
    }


    public static ResultVo fail(String message, String errorMessage) {
        ResultVo result = new ResultVo();
        result.setCode(400);
        result.setMessage(message);
        result.setData(errorMessage);
        return result;
    }
    public static <T> ResultVo<T> fails(ResultCodeEnum resultCode, String message) {
        return new ResultVo<>(resultCode, message, null);
    }

    public static<T> ResultVo<T> ok(){

        return ResultVo.ok(null);
    }
    public static<T> ResultVo<T> ok(T data){
        return build(data, ResultCodeEnum.SUCCESS);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> ResultVo<T> success(T data){

        return build(data, ResultCodeEnum.SUCCESS);

    }

    public static<T> ResultVo<T> fail(){
        return ResultVo.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> ResultVo<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }

    public ResultVo<T> message(String msg){
        this.setMessage(msg);

        return this;
    }

    public ResultVo<T> code(Integer code){
        this.setCode(code);
        return this;
    }

}

